package com.jk.dmconsumer.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jk.dmconsumer.entity.SiteEntity;
import jdk.nashorn.internal.runtime.JSONFunctions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名称:TestController
 *
 * @Description //TODO
 * @Date 2021/9/17 10:54
 * 作者 王乐
 * Copyright (c) 2021,  All Rights Reserved.</pre>
 */
@RestController
public class TestController {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
    *  @方法描述 (存消息)
     * @Author 王乐
     * @Description //TODO 
     * @Date  2021/9/17 11:03
     * @Param [info1]
     * @return void
     **/
    @RequestMapping("home/sendForm")
    public void sendForm(String info1){
       /* ArrayList<String> list = new ArrayList<>();
        list.add(info1);
        Boolean boo = redisTemplate.hasKey("test917");
        if(boo){
            ArrayList<String> list1  = (ArrayList<String>) redisTemplate.opsForHash().get("test917","info");
            list1.add(info1);
        }else {
            redisTemplate.opsForHash().put("test917","info",list);
        }*/
        redisTemplate.opsForHash().put("test917s1","infos1",info1);
    }
    @RequestMapping("home/getMess")
    public String getMess(){
        ArrayList<String> list  = (ArrayList<String>) redisTemplate.opsForHash().get("test917","info");
        String str = (String) redisTemplate.opsForHash().get("test917s1","infos1");
        redisTemplate.opsForHash().delete("test917s1","infos1");
        return str;
    }



    @RequestMapping("home/sendForm2")
    public void sendForm2(String info2){
        /*ArrayList<String> list = new ArrayList<>();
        list.add(info2);
        Boolean boo = redisTemplate.hasKey("test9172");
        if(boo){
            ArrayList<String> list1  = (ArrayList<String>) redisTemplate.opsForHash().get("test9172","info2");
            list1.add(info2);
        }else {
            redisTemplate.opsForHash().put("test9172","info2",list);
        }*/
        redisTemplate.opsForHash().put("test917s2","infos2",info2);
    }
    @RequestMapping("home/getMess2")
    public String getMess2(){
       // ArrayList<String> list  = (ArrayList<String>) redisTemplate.opsForHash().get("test9172","info2");
        String str = (String) redisTemplate.opsForHash().get("test917s2","infos2");
        redisTemplate.opsForHash().delete("test917s2","infos2");
        return str;
    }
    /**
    *  @方法描述 (数组存到redis)
     * @Author 王乐
     * @Description //TODO 
     * @Date  2021/9/17 17:43
     * @Param [site]
     * @return void
     **/
    @RequestMapping("/test/saveArr")
    public void saveArr(SiteEntity site){
        redisTemplate.opsForHash().put("arr","arr",site);
    }
    /**
    *  @方法描述 (确认座位)
     * @Author 王乐
     * @Description //TODO 
     * @Date  2021/9/17 20:52
     * @Param [siteArr]
     * @return java.lang.String
     **/
    @RequestMapping("/test/confirmSite")
    public String confirmSite(String siteArr){
        //json字符串转集合
        List<int[][]> ints1 = JSONArray.parseArray(siteArr, int[][].class);
        //集合转成数组
        int[][][] intArr = new int[ints1.size()][][];
        for (int i = 0;i<ints1.size();i++){
            intArr[i]=ints1.get(i);
        }
        intArr = intArr;
        //数组转成json
        String s = JSONArray.toJSONString(intArr);
        System.out.println(s);
        //把测试数组存redis
        redisTemplate.opsForHash().put("dmsite","site1",s);
        /*JSONArray parse = JSONArray.parseArray(siteArr);
        int[][][] ints = new int[parse.size()][][];
        int i = 0;
        for (Object parse2: parse) {
            List<int[]> list = (List<int[]>) parse2;
            for (int[] num: list) {

                System.out.println(num);
            }
            int[][] intarr = new int[list.size()][];
            int[][] arr = list.toArray(new int[list.size()][]);
            ints[i++]=arr;
            System.out.println("123");

        }
        String s = JSON.toJSONString(ints);

        Integer[][][] arr = new Integer[5][5][2];

        Object integers = parse.toArray(new Integer[0][][]);*/
        return null;
    }
}
